package com.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dao.UserDAO;
import com.example.entity.User;

public class ExampleServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// fetch the rows ...
		List<User> users = null;
		try {
			users = UserDAO.getUsers();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// ... generate html
		PrintWriter writer = response.getWriter();
		writer.println("<html><head><title>Example Servlet</title></head><body><h1>example servlet: the first 10 users are ...</h1>");
		
		if (users != null) {
			for (int i = 0; i < users.size(); i++) {
				writer.println(users.get(i).getForename() + " " + users.get(i).getSurname() + "<br/>");
			}
		}
		
		writer.println("<body></html>");	
		writer.close();	
	}

}
